package menu
{
	import com.*;	
	import data.*;
	import events.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import org.flixel.*;
	import ui.*;
	import ui.popup.GCTextPopup;
	import ui.textfield.*;
	import ui.textfield.GCTextField;
	
	/**
	 * Menu of the city that the player is currently docked at.
	 * 
	 */ 
	public class GCCityMenu extends Sprite implements GCSuperMenu
	{
		/*
		Labels for the buttons.
		*/
		private static const MARKET_LABEL:String = "Marketplace\nBuy and sell goods";
		private static const INVENTORY_LABEL:String = "Warehouse Inventory\nGoods stored in this city!";
		private static const BUILD_LABEL:String = "Trade Post\nConstruct buildings here!";
		private static const ASSIGN_LABEL:String = "Assign Workers\n";
		
		/*
		Buttons in the city menu
		*/
		private var _cityMarketButton:Sprite;
		private var _cityInventoryButton:Sprite;
		private var _cityBuildPostButton:Sprite;
		private var _cityAssignJobsButton:Sprite;
		
		/*
		Labels for the buttons
		*/
		private var _cityMarketLabel:GCStylizedText;
		private var _cityInventoryLabel:GCStylizedText;
		private var _cityBuildPostLabel:GCStylizedText;
		private var _cityAssignJobsLabel:GCStylizedText;
		
		/*
		 * City Data
		 */
		private var _cityData:GCCity;
		private var _cityText:GCTextField;
		
		/*
		 * Stuff to help blink the button
		 */
		private var _blinkTimer:Timer;
		private var _blinkTarget:Sprite;
		private var _blinkDelta:Number;
		
		public function GCCityMenu()
		{
			super();
			
			_cityMarketButton = new Sprite();
			_cityInventoryButton = new Sprite();
			_cityBuildPostButton = new Sprite();
			_cityAssignJobsButton = new Sprite();
			
			_cityMarketLabel = new GCStylizedText();
			_cityInventoryLabel = new GCStylizedText();
			_cityBuildPostLabel = new GCStylizedText();
			_cityAssignJobsLabel = new GCStylizedText();
			
			_cityText = new GCTextField();
			_blinkTimer = new Timer(100);
			
			init();
			
			var background:Bitmap = GCResources.getResource("main_background");
			this.addChildAt(background, 0);
			
			//this.graphics.beginFill(0xffffff);
			//this.graphics.drawRect(0, 0, GCMainDisplay.WIDTH, GCMainDisplay.HEIGHT);
			//this.graphics.endFill();
		}
		
		/**
		 * Function that should be called right before the menu
		 * is added to the display hierarchy to set up its
		 * appearance. Pass proper parameters so the correct parts show
		 * up.
		 */
		public function openMenu(cityData:GCCity):void
		{
			// Roy--
			// tutorial
			if (GCPlayState.tutorialSequence.currentTutorialStage == GCTutorial.STAGE_VERY_START)
			{
				if (GCConstants.DEBUG_MODE)
				{
					completedTutorialStart();
				}
				else
				{
					GCUtil.updateTutorialProgress(GCPlayState.playerData.getPlayerId(), GCPlayState.tutorialSequence.currentTutorialStage + 1,
					function(code:uint, data:Object):void
					{
						completedTutorialStart();
					});
				}
				
				function completedTutorialStart():void
				{
					GCPlayState.tutorialSequence.goToNextStage();
					GCPlayState.disableScreen(new GCTextPopup(GCTutorial.CITY_INTRODUCTION_MESSAGE, GCPlayState.enableScreen));
					GCPlayState.tutorialSequence.dispatchEvent(new GCQuestEvent(GCEvent.ADD_NEW_QUESTS_EVENT));
				}
			}
			
			/*
			 * Market is always visible
			 */
			_cityMarketButton.visible = true;
			
			/*
			 * Only show the inventory button if the player has a warehouse at the
			 * city.
			 */
			if (cityData.getInventoryFromCity() != null)
			{
				_cityInventoryButton.visible = true;
			}
			
			/*
			 * Build post is hidden only for certain tutorial sequences.
			 */
			if (GCPlayState.tutorialSequence.currentTutorialStage >= GCTutorial.STAGE_BUILD_GATHER)
			{
				_cityBuildPostButton.visible = true;
			}
				
			/*
			 * Only show the assign jobs option if the player has at least one harvest
			 * building constructed.
			 */
			for each (var bldgId:uint in cityData.getAllAlreadyConstructed())
			{
				if (GCBuildingData.getBuildingTypeFromId(bldgId) == GCBuildingData.BUILDING_TYPE_HARVEST)
				{
					_cityAssignJobsButton.visible = true;
					break;
				}
			}
			
			_cityData = cityData;
			_cityText.setStyle(GCTextField.TEXT_TYPE_LEVEL);
			_cityText.textColor = (GCConstants.COLOR_GOLD);
			_cityText.text = "You are now in " + _cityData.getName();
			
			/*
			 * Blinking the buttons at appropriate stages of the tutorial.
			 */
			if (GCPlayState.tutorialSequence.currentTutorialStage == GCTutorial.STAGE_ON_RETURN)
			{
				if (GCPlayState.tutorialSequence.getTutorialQuestFromTemplate(GCQuestData.QUEST_TEMPLATE_TUTORIAL_18) != null)
				{
					startBlinkButton(_cityInventoryButton);
				}
			}
			else if (GCPlayState.tutorialSequence.currentTutorialStage == GCTutorial.STAGE_BUILD_GATHER)
			{
				if (GCPlayState.tutorialSequence.getTutorialQuestFromTemplate(GCQuestData.QUEST_TEMPLATE_TUTORIAL_12) != null)
				{
					startBlinkButton(_cityBuildPostButton);
				} else if (GCPlayState.tutorialSequence.getTutorialQuestFromTemplate(GCQuestData.QUEST_TEMPLATE_TUTORIAL_13) != null)
				{
					startBlinkButton(_cityAssignJobsButton);
				}
			}
		}
		
		/**
		 * Function that should be called after the menu is removed
		 * from the display hierarchy.
		 */
		public function closeMenu():void
		{
			_cityMarketButton.visible = false;;
			_cityInventoryButton.visible = false;
			_cityBuildPostButton.visible = false;
			_cityAssignJobsButton.visible = false;
			
			// Stop any blink effects when leaving the menu
			stopBlinkButton();
		}
		
		private function init():void
		{
			var bmd:BitmapData;
			var bitmap:Bitmap;
			
			_cityMarketButton.addEventListener(MouseEvent.CLICK, onMarketClick);
			_cityMarketButton.addEventListener(MouseEvent.MOUSE_OVER, onActivateButton);
			_cityMarketButton.addEventListener(MouseEvent.MOUSE_OUT, onDeactivateButton);
			_cityInventoryButton.addEventListener(MouseEvent.CLICK, onInventoryClick);
			_cityInventoryButton.addEventListener(MouseEvent.MOUSE_OVER, onActivateButton);
			_cityInventoryButton.addEventListener(MouseEvent.MOUSE_OUT, onDeactivateButton);
			_cityBuildPostButton.addEventListener(MouseEvent.CLICK, onBuildClick);
			_cityBuildPostButton.addEventListener(MouseEvent.MOUSE_OVER, onActivateButton);
			_cityBuildPostButton.addEventListener(MouseEvent.MOUSE_OUT, onDeactivateButton);
			_cityAssignJobsButton.addEventListener(MouseEvent.CLICK, onAssignClick);
			_cityAssignJobsButton.addEventListener(MouseEvent.MOUSE_OVER, onActivateButton);
			_cityAssignJobsButton.addEventListener(MouseEvent.MOUSE_OUT, onDeactivateButton);
			
			_blinkTimer.addEventListener(TimerEvent.TIMER, onBlink);
			
			_cityMarketLabel.text = MARKET_LABEL;
			_cityMarketLabel.setBgStyle(1, 200, 50);
			_cityInventoryLabel.text = INVENTORY_LABEL;
			_cityInventoryLabel.setBgStyle(1, 200, 50);
			_cityBuildPostLabel.text = BUILD_LABEL;
			_cityBuildPostLabel.setBgStyle(1, 200, 50);
			_cityAssignJobsLabel.text = ASSIGN_LABEL;
			_cityAssignJobsLabel.setBgStyle(1, 200, 50);
			
			_cityMarketButton.x = 100;
			_cityMarketButton.y = 300;
			bmd = FlxG.addBitmap(GCResources.marketButton);
			bitmap = new Bitmap(bmd);
			_cityMarketButton.addChild(bitmap);
			//_cityMarketLabel.x = _cityMarketButton.width / 2 - _cityMarketLabel.width / 2;
			_cityMarketLabel.y = _cityMarketButton.height;
			_cityMarketButton.addChild(_cityMarketLabel);
			
			_cityInventoryButton.x = 450;
			_cityInventoryButton.y = 300;
			bmd = FlxG.addBitmap(GCResources.warehouseButton);
			bitmap = new Bitmap(bmd);
			_cityInventoryButton.addChild(bitmap);
			//_cityInventoryLabel.x = _cityInventoryButton.width / 2 - _cityInventoryLabel.width / 2;
			_cityInventoryLabel.y = _cityInventoryButton.height;
			_cityInventoryButton.addChild(_cityInventoryLabel);
			
			_cityBuildPostButton.x = 100;
			_cityBuildPostButton.y = 70;
			bmd = FlxG.addBitmap(GCResources.tradepostButton);
			bitmap = new Bitmap(bmd);
			_cityBuildPostButton.addChild(bitmap);
			//_cityBuildPostLabel.x = _cityBuildPostButton.width / 2 - _cityBuildPostLabel.width / 2;
			_cityBuildPostLabel.y = _cityBuildPostButton.height;
			_cityBuildPostButton.addChild(_cityBuildPostLabel);
			
			_cityAssignJobsButton.x = 450;
			_cityAssignJobsButton.y = 70;
			bmd = FlxG.addBitmap(GCResources.workerButton);
			bitmap = new Bitmap(bmd);
			_cityAssignJobsButton.addChild(bitmap);
			//_cityAssignJobsLabel.x = _cityAssignJobsButton.width / 2 - _cityAssignJobsLabel.width / 2;
			_cityAssignJobsLabel.y = _cityAssignJobsButton.height;
			_cityAssignJobsButton.addChild(_cityAssignJobsLabel);

			_cityText.width = FlxG.width;
			_cityText.height = 50;
			
			this.addChild(_cityText);
			this.addChild(_cityMarketButton);
			this.addChild(_cityInventoryButton);
			this.addChild(_cityBuildPostButton);
			this.addChild(_cityAssignJobsButton);

			_cityMarketButton.visible = false;;
			_cityInventoryButton.visible = false;
			_cityBuildPostButton.visible = false;
			_cityAssignJobsButton.visible = false;
		}
		

		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------		
		
		private function onMarketClick(event:Event):void
		{
			trace("onMarketClick");
			dispatchEvent(new GCEvent(GCEvent.GO_TO_MARKET_EVENT));
		}
		
		private function onInventoryClick(event:Event):void
		{
			trace("onWarehouseClick");
			dispatchEvent(new GCEvent(GCEvent.GO_TO_WAREHOUSE_EVENT));
		}
		
		private function onBuildClick(event:Event):void
		{
			trace("onBuildPostClick");
			dispatchEvent(new GCEvent(GCEvent.GO_TO_BUILD_POST_EVENT));
		}
		
		private function onAssignClick(event:Event):void
		{
			trace("onAssignJobsClick");
			dispatchEvent(new GCEvent(GCEvent.GO_TO_ASSIGN_JOB_EVENT));
		}
		
		private function onActivateButton(event:MouseEvent):void
		{
			var button:Sprite = event.currentTarget as Sprite;
			button.graphics.beginFill(GCConstants.COLOR_TAN, 0.7);
			button.graphics.drawRoundRect(0, 0, button.width, button.height, 5);
			button.graphics.endFill();
		}
		
		private function onDeactivateButton(event:MouseEvent):void
		{
			var button:Sprite = event.currentTarget as Sprite;
			button.graphics.clear();
		}
		
		private function startBlinkButton(target:Sprite):void
		{
			_blinkTarget = target;
			_blinkTimer.start();
			_blinkDelta = -0.15;
		}
		
		private function stopBlinkButton():void
		{
			if (_blinkTarget != null)
			{
				_blinkTimer.stop();
				_blinkTarget.alpha = 1.0;
				_blinkTarget = null;
			}
		}
		
		private function onBlink(event:TimerEvent):void
		{
			if (_blinkTarget.alpha > 1.0 && _blinkDelta > 0)
			{
				_blinkDelta *= -1;
			}
			else if (_blinkTarget.alpha < 0.4 && _blinkDelta < 0)
			{
				_blinkDelta *= -1;
			}
			_blinkTarget.alpha += _blinkDelta;
		}
		
		//----------------------------------------------------------------------
		//	SUPER MENU FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 * Method to call to refresh any dynamic parts on the menu
		 */
		public function refresh():void
		{
			openMenu(this._cityData);
		}
		
		/**
		 * Method to enable list displays to scroll
		 */
		public function enableScrolling():void
		{
			
		}
		
		/**
		 * Method to disable list displays from scrolling
		 */
		public function disableScrolling():void
		{
			
		}
		
	}
}